package cn.midas.basic.service.impl;

import cn.midas.basic.constant.BaseConstants;
import cn.midas.basic.dto.RejisterDto;
import cn.midas.basic.exception.BusinessException;
import cn.midas.basic.service.IGraphCodeService;
import cn.midas.basic.utils.SmsUtils;
import cn.midas.basic.utils.StrUtils;
import cn.midas.basic.utils.VerifyCodeUtils;
import cn.midas.user.domain.User;
import cn.midas.user.mapper.UserMapper;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class GraphCodeServiceImpl implements IGraphCodeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;



    /*/**
    * @Title: graph
    * @Description: 发送图片验证码 q
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/6 15:32
    * @Parameters: [imageCodeKey]
    * @Return java.lang.String
    */
    @Override
    public String graph(String imageCodeKey) {
        // 判断key值是否为空 为空抛出自定义异常
            if(StringUtils.isEmpty(imageCodeKey)){
                throw new RuntimeException("参数值为空,请检查");
            }
        // 调用字符串工具类 获取四位随机字符
        String code = StrUtils.getComplexRandomString(4);
        // 把key 和随机四位字符 设置到redis中
        redisTemplate.opsForValue().set(imageCodeKey,code,5, TimeUnit.MINUTES);
        // 将code 值变成图形并编码base64
        String base64Str = VerifyCodeUtils.VerifyCode(100, 35, code);
        // 返回图形base64码
        return base64Str;
    }

    /*/**
    * @Title: smsCode
    * @Description: 发送手机验证码
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/7/6 15:31
    * @Parameters: [rejisterDto]
    * @Return void
    */
    @Override
    public void smsCode(RejisterDto rejisterDto) {
        String imageCodeKey = rejisterDto.getImageCodeKey();
        String phone = rejisterDto.getPhone();
        String imageCodeValue = rejisterDto.getImageCodeValue();
        //1.判断参数为空校验
        if(StringUtils.isEmpty(imageCodeKey) || StringUtils.isEmpty(phone) ||StringUtils.isEmpty(imageCodeValue)){
            throw new BusinessException("参数为空，请填写");
        }
        //2.判断手机号是否存在用户是否已经被注册
        User user = userMapper.loadByPhone(phone);
        if (user != null){
            throw new BusinessException("用户已经存在，请直接登录！");
        }
        //3.判断图形验证码是否存在、是否正确
        Object imageCodeTmp = redisTemplate.opsForValue().get(imageCodeKey);
        if (imageCodeTmp == null){
            throw new BusinessException("图形验证码不存在！");
        }//equalsIgnoreCase
        if (!imageCodeTmp.toString().equalsIgnoreCase(imageCodeValue)){
            throw new BusinessException("图形验证码错误，请重新填写！");
        }
        //4.前面都通过 用户需要获取新的手机验证码 不能直接发验证码要做业务处理
        //4.1使用业务键拼接手机号从redis中获取到手机验证码register：phone
       String smsCodeKye = BaseConstants.GraphCodeConstants.BUSINESS_REGISTER_PREFIX + phone;
        Object smsCodeValueTmp = redisTemplate.opsForValue().get(smsCodeKye);
        //4.2判断验证码是否为空，为空说明已经过期，生成新的验证码
        String smsCode ="";//存放重新发送验证码
        if (smsCodeValueTmp == null){
            smsCode = StrUtils.getComplexRandomString(4);
        }else {
            String[] smsCodeValueArray = smsCodeValueTmp.toString().split(":");
            Long oldTime = Long.valueOf(smsCodeValueArray[1]);
            long newTime = System.currentTimeMillis();
            // 判断前后时间戳是否大于一分钟
            if (newTime-oldTime<1*60*1000){
                throw new BusinessException("请一分钟后重试！");
            }
            smsCode = smsCodeValueArray[0];
        }
        //5.设置手机验证码到redis中
        String smsCodeValue = smsCode + ":" + System.currentTimeMillis();
        redisTemplate.opsForValue().set(smsCodeKye,smsCodeValue,3,TimeUnit.MINUTES);
        //6.发送手机短信验证码
       // SmsUtils.sendMsg(phone,"您的验证码是："+smsCode);
        System.out.println("您的验证码是："+smsCode);
    }
}
